package com.itstyle.es.log.controller;
import java.sql.Timestamp;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.itstyle.es.common.utils.JsonMapper;
import com.itstyle.es.log.entity.Pages;
import com.itstyle.es.log.entity.SysLogs;
import com.itstyle.es.log.service.LogService;
@Controller
@RequestMapping(value = "log")
public class LogController {
   
   @Autowired
   private  ElasticsearchTemplate elasticSearchTemplate;
   @Autowired
   private LogService logService;

   
   @GetMapping(value="index")
   public String  index() {
		 return "log/index";
   }
   @PostMapping(value="list")
   public @ResponseBody Pages<SysLogs>  list(Integer pageNumber,Integer pageSize,
		 Integer platFrom,String searchContent) {
	  return logService.searchLogPage(pageNumber, pageSize, platFrom, searchContent);
   }
   /**
    * redis 日志队列测试接口
    */
   @GetMapping(value="add")
   public @ResponseBody String redisLog() {
	    SysLogs log = new SysLogs();
		log.setUsername("小马云");
		log.setOperation("删除");
		log.setMethod("com.itstyle.es.log.controller.index()");
		log.setIp("192.168.1.70");
		log.setGmtCreate(new Timestamp(new Date().getTime()));
		log.setExceptionDetail("开源中国社区");
		log.setParams("{'name':'码云','type':'开源'}");
		log.setDeviceType((short)1);
		log.setPlatFrom((short)1);
		log.setLogType((short)1);
		log.setDeviceType((short)1);
		log.setId(System.currentTimeMillis());
		log.setUserId((long)1);
		log.setTime((long)1);
	   logService.saveLog(log);
		//模拟日志队列实现
		return "success";
   }
	@PostMapping(value="delete")
	public @ResponseBody String delete(@RequestBody SysLogs log) {
		logService.deleteLog(log);
		//模拟日志队列实现
		return "success";
	}
   /**
    * kafka 日志队列测试接口
    */
   @GetMapping(value="kafkaLog")
   public @ResponseBody String kafkaLog() {
	    SysLogs log = new SysLogs();
		log.setUsername("红薯");
		log.setOperation("删除");
		log.setMethod("com.itstyle.es.log.controller.kafkaLog()");
		log.setIp("192.168.1.80");
		log.setGmtCreate(new Timestamp(new Date().getTime()));
		log.setExceptionDetail("开源中国社区");
		log.setParams("{'name':'码云','type':'开源'}");
		log.setDeviceType((short)1);
		log.setPlatFrom((short)1);
		log.setLogType((short)2);
		log.setDeviceType((short)2);
		log.setId(System.currentTimeMillis());
		log.setUserId((long)1);
		log.setTime((long)1);
		//模拟日志队列实现
	   logService.saveLog(log);
		return "success";
   }
}
